﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace BBPA.Admin.SYS.Accountant
{
    public partial class Form : FormPage
    {
        BLL.Accountant bll = new BLL.Accountant();
        Model.Accountant model = new Model.Accountant();
        //
        BLL.PAUser bllUser = new BLL.PAUser();
        Model.PAUser modelUser = new Model.PAUser();
        protected void Page_Load(object sender, EventArgs e) { }
        /// <summary>
        /// 初始化本页Model
        /// </summary>
        public override void Page_Load_DefaultModel() { model = bll.GetModel(Master.id); modelUser = bllUser.GetModel(model.UserID); }
        /// <summary>
        /// 初始化表单
        /// </summary>
        public override void Form_Default()
        {
        }
        /// <summary>
        /// 绑定表单数据【修改时用】
        /// </summary>
        public override void Form_Bind()
        {
            tb_Password_helper.Visible = true;

            tb_Realname.Text = model.Realname;
            tb_Mobile.Text = model.Mobile;

            cb_IsLock.Checked = modelUser.IsLock;
        }
        /// <summary>
        /// 提交
        /// </summary>
        public override void Form_Submit()
        {
            string Realname = PageValidate.SqlTextClear(DataTypePlus.requestString(tb_Realname.Text));
            string Password = PageValidate.SqlTextClear(tb_Password.Text); ;
            string mobile=PageValidate.SqlTextClear(tb_Mobile.Text);
            if (
                Master.noEmpty(Realname, "[姓名]必须填写") &&
                Master.noChongfu("Realname='" + Realname + "'", "[姓名]重复")
                )
            {
                modelUser.Username = Realname;
                modelUser.IsLock = cb_IsLock.Checked;
                modelUser.Mobile=mobile;

                model.Realname = Realname;
                model.Mobile = mobile;
                if (Master.formState == EnumHelper.FormState.add)
                {
                    //添加User
                    modelUser.Password = StringPlus.md5(Password);
                    modelUser.UserType = (int)EnumHelper.UserType.Accountant;
                    modelUser.State = (int)EnumHelper.UserState.Nomal;
                    int UserID_new = bllUser.Add(modelUser);
                    //添加Accountant
                    model.UserID = UserID_new;
                    //Rank
                    List<Model.Accountant> listModelAccountant = bll.GetModelList(" id>0  order by Rank desc");
                    if (listModelAccountant != null && listModelAccountant.Count > 0) model.Rank = listModelAccountant[0].Rank + 1;
                    else model.Rank = 1;
                    //Number
                    model.Number = model.Rank.ToString().PadLeft(2, '0');//2
                    Master.id = bll.Add(model);
                    //更新Accountant.Number
                    model = bll.GetModel(Master.id);
                    model.Number = "A-" + (bll.GetRecordCount(""));
                    bll.Update(model);
                }
                else
                {
                    if (Password != "") modelUser.Password = StringPlus.md5(Password);
                    bllUser.Update(modelUser);
                    bll.Update(model);
                }

                BLL.User_Operate_Log.Add(Security.GetCurrentUserID(), EnumHelper.TableName.Accountant, Master.id, Master.formState == EnumHelper.FormState.add ? EnumHelper.OperateType.Add : EnumHelper.OperateType.Update, "");
                
                Master.SubmitOK();

            }
        }
    }
}